// 观察者模式 

// 被观察者 存放观察者 

class Subject{
    constructor(name){
        this.name = name
        this.users = []
        this.state = '开心'
    }
    attach(v){
        this.users.push(v)
    }
    setState(newState){
        this.state = newState
        this.users.forEach(v => {
            v.update(this)
        });
    }
}

class Observer{
    constructor(name){
        this.name = name
    }
    update(baby){
        console.log('name',this.name,'baby状态',baby.state);
    }
}

const baby = new Subject('baby')

const father = new Observer('爸爸')

const mother = new Observer('妈妈')

baby.attach(father)
baby.attach(mother)
baby.setState('被欺负了')
